package com.xyh.jvmDemo;

import java.util.concurrent.locks.ReentrantLock;

public class LockDemo implements Runnable {
    volatile int tickets = 100;
    private ReentrantLock lock = new ReentrantLock(true);

    // private Object obj = new Object();
    @Override
    public void run() {
        while (true) {
            lock.lock();
            try {
                if (tickets <= 0) {
                    break;
                } else {
                    Thread.sleep(1);
                    tickets--;
                    System.out.println(Thread.currentThread().getName() + "正在售票,还剩下" + tickets + "张");

                }
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                lock.unlock();
            }
        }
    }
}